<script language="javascript" type="text/javascript">
var groups_id="";
function groups_a()
{
	var pma=upperStr($("#groups_a_table #ma").val());
	var pten=properStr1Char($("#groups_a_table #ten").val());
	$("#fgroups_add").dialog("option","disabled",true);
	$.ajax({
		url:"modules/groups/a.php",
		type:"POST",
		dataType:"xml",
		data:"ma="+encodeURI(pma)+"&ten="+encodeURI(pten),
		success:function(xml){
			var status=$(xml).find("status").text();
			var message=$(xml).find("message").text();
			 msgbox(status,message);
		
			if(status=="ok") 
			{
				groups_id=pma;
				$("#fgroups_add").dialog("close");
			}
			else $("#fgroups_add").dialog("option","disabled",false);
		}
	});
}
//-----------------------------------------------------------------------------------------
function groups_af()
{
	var fgroupsoptions={
		width:420,
		title:"Thêm nhóm người dùng ",
		modal:true,
		buttons:{
			"Thêm":groups_a,
			"Hủy":function(){$("#fgroups_add").dialog("close");}
		},
		disabled:true,
		focus:groups_af_ah,
		open:rebuildACL,
		close:function(){
			$("#fgroups_add").remove();
			$("#groups_flex").flexReload();
			groups_af_dh();
		}
	};
	
	$("body").append("<div id=\"fgroups_add\" style=\"display:none;\"></div>");

	$("#fgroups_add").dialog(fgroupsoptions);
	$("#fgroups_add").load("modules/groups/af.php","",function(){
		$("#fgroups_add").dialog("option","disabled",false);
		$("#fgroups_add").dialog("option","position","center");
	});
}
//-----------------------------------------------------------------------------------------
function groups_u()
{
	var pma=upperStr($("#groups_u_table #ma").val());
	var pten=properStr1Char($("#groups_u_table #ten").val());
	$("#fgroups_update").dialog("option","disabled",true);
	$.ajax({
		url:'modules/groups/u.php',
		type:"POST",
		dataType:"xml",
		data:'ma='+encodeURI(pma)+'&ten='+encodeURI(pten)+'&macu='+$('#groups_u_table #macu').val(),
		success:function(xml){
			var status=$(xml).find("status").text();
			var message=$(xml).find("message").text();
			msgbox(status,message);
			if(status=="ok") 
			{
				groups_id=pma;
				$("#fgroups_update").dialog("close");
			}
			else $("#fgroups_update").dialog("option","disabled",false);
		}
	});
}
//-----------------------------------------------------------------------------------------
function groups_uf()
{
	if(!$("#groups_flex .trSelected").attr("id"))
	{
		msgbox("fail","Phải chọn một mục để cập nhật !");
		return;
	}
	var sma=$("#groups_flex .trSelected").attr("id");
	sma = sma.substring(sma.lastIndexOf("row")+3);

	
	$.ajax({
		url:"modules/groups/s.php",
		type:"POST",
		dataType:"xml",
		data:"ma="+sma,
		success:function(xml){
			
			var k=$(xml).find("row");
			
			//alert(k.text());
			var fgroupsoptions={
						width:520,
						title:"Cập nhật nhóm người dùng ",
						modal:true,
						buttons:{
							"Cập nhật":groups_u,
							"Hủy":function(){$("#fgroups_update").dialog("close");}
						},
						disabled:true,
						focus:groups_uf_ah,
						open:rebuildACL,
						close:function(){
							$("#fgroups_update").remove();
							$("#groups_flex").flexReload();
							groups_uf_dh();
						}
					};

					$("body").append("<div id=\"fgroups_update\" style=\"display:none;\"></div>");
					$("#fgroups_update").dialog(fgroupsoptions);	
					$("#fgroups_update").load("modules/groups/uf.php","",function(){
						$("#groups_u_table #ma").attr("value",sma);
						$("#groups_u_table #macu").attr("value",sma);

						if(k[0].childNodes[1].text) $("#groups_u_table #ten").val(k[0].childNodes[1].text);
						else $("#groups_u_table #ten").val(k[0].childNodes[1].textContent);

						$("#fgroups_update").dialog("option","disabled",false);
						$("#fgroups_update").dialog("option","position","center");
					});
				}
	});
}
//-----------------------------------------------------------------------------------------
function groups_df()
{
	if(!$("#groups_flex .trSelected").attr("id"))
	{
		msgbox("fail","Phải chọn một mục để xoá !");
		return;
	}
	var sma=$("#groups_flex .trSelected").attr("id");
	sma = sma.substring(sma.lastIndexOf("row")+3);
	$.confirmDialog({
			msg:'Có chắc chắn xoá nhóm người dùng  '+sma+' không ?',
			yes:"Có",
			no:"Không",
			title:"Xác nhận xoá"
		},
		function(){
			$("#fgroups").dialog("option","disabled",true);
			$.ajax({
				url:'modules/groups/d.php',
				type:"POST",
				dataType:"xml",
				data:'ma='+sma,
				success:function(xml){
					var status=$(xml).find("status").text();
					var message=$(xml).find("message").text();
					
					msgbox(status,message);
					if(status=="ok") $("#groups_flex").flexReload();
					$("#fgroups").dialog("option","disabled",false);
					groups_ah();
				}
			});		
		},function(){
			groups_ah();
		});
}
/*--------------------------------------------------------------------------------------*/
function groups_af_hk(e)
{
	var code=(e.keyCode ? e.keyCode : e.which);
	if(code==13)
	{
		$("#fgroups_add").dialog("option","buttons")["Thêm"]();
	}
	else if(code==27)
	{
		$("#fgroups_add").dialog("close");
	}
	e.stopPropagation();
	e.preventDefault();
}
/*--------------------------------------------------------------------------------------*/
function groups_uf_hk(e)
{
	var code=(e.keyCode ? e.keyCode : e.which);
	if(code==13)
	{
		$("#fgroups_update").dialog("option","buttons")["Cập nhật"]();
	}
	else if(code==27)
	{
		$("#fgroups_update").dialog("close");
	}
	e.stopPropagation();
	e.preventDefault();
}
/*--------------------------------------------------------------------------------------*/
function groups_af_ah()
{
	$(document).off("keydown");
	$(document).on("keydown",groups_af_hk);
}
/*--------------------------------------------------------------------------------------*/
function groups_af_dh()
{
	$(document).off("keydown");
	$(document).on("keydown",groups_hk);
}
/*--------------------------------------------------------------------------------------*/
function groups_uf_ah()
{
	$(document).off("keydown");
	$(document).on("keydown",groups_uf_hk);
}
/*--------------------------------------------------------------------------------------*/
function groups_uf_dh()
{
	$(document).off("keydown");
	$(document).on("keydown",groups_hk);
}
/*--------------------------------------------------------------------------------------*/
function groups_hk(e)
{
	var code=(e.keyCode ? e.keyCode : e.which);
	//Down
	if(code==38)
	{
		if($("#groups_flex tr.trSelected").is("#groups_flex tr:first")) return;
		var id=$("#groups_flex tr.trSelected").prev().attr("id");
		$("#groups_flex tr.trSelected").removeClass("trSelected");
		$("#groups_flex #"+id).addClass("trSelected");
	}
	// Up
	else if(code==40)
	{
		if($("#groups_flex tr.trSelected").is("#groups_flex tr:last")) return;
		var id=$("#groups_flex tr.trSelected").next().attr("id");
		$("#groups_flex tr.trSelected").removeClass("trSelected");
		$("#groups_flex #"+id).addClass("trSelected");
	}
	//Enter
	else if(code==13)
	{
		$("#fgroups").dialog("option", "buttons")["Sửa"]();
	}
	// Xóa
	else if(code==46)
	{
		$("#fgroups").dialog("option", "buttons")["Xoá"]();
	}
	// +
	else if(code==107)
	{
		$("#fgroups").dialog("option", "buttons")["Thêm"]();
	}
	e.stopPropagation();
	e.preventDefault();
}
/*--------------------------------------------------------------------------------------*/
function groups_ah()
{
	$(document).off("keydown");
	$(document).on("keydown",groups_hk);
	if(event!=null) if(typeof(event)!="undefined")
	{
		event.stopPropagation();
		event.preventDefault();
	}
}
/*--------------------------------------------------------------------------------------*/
function groups_dh()
{
	$(document).off("keydown");
}
//----------------------------------------------------------------------------------------
function groups()
{
	if($("#fgroups").is(":data(dialog)")) return;

	$("body").append("<div id=\"fgroups\"></div>");
	
	var fgroupsoptions={
		width:520,
		title:"Danh mục nhóm người dùng",
		disabled:true,
		buttons:{
			"Quyền hạn":quyengroups,
			"Thêm":groups_af,
			"Sửa":groups_uf,
			"Xoá":groups_df
		},
		focus:groups_ah,
		close:function(){
			$("#fgroups").remove();
			groups_dh();
		}
	};
	$("#fgroups").dialog(fgroupsoptions);
	$("#fgroups").load("modules/groups/g.php","",function(){
		$("#fgroups").dialog("option","disabled",false);
	});
}
</script>
